package com.cdream.basic.algorithm.leetcode.stack;

import java.util.LinkedList;

/**
 * todo
 *
 * @author cdream
 * @date 6/26/21
 */
public class Question150 {
    public int evalRPN(String[] tokens) {
        LinkedList<String> strings = new LinkedList<>();
        for (String token : tokens) {
            switch (token) {
                case "/":
                    String pop1 = strings.pop();
                    String pop2 = strings.pop();
                    strings.push(String.valueOf(Integer.parseInt(pop2) / Integer.parseInt(pop1)));
                    break;
                case "*":
                    String pop3 = strings.pop();
                    String pop4 = strings.pop();
                    strings.push(String.valueOf(Integer.parseInt(pop3) * Integer.parseInt(pop4)));
                    break;
                case "+":
                    String pop5 = strings.pop();
                    String pop6 = strings.pop();
                    strings.push(String.valueOf(Integer.parseInt(pop5) + Integer.parseInt(pop6)));
                    break;
                case "-":
                    String pop7 = strings.pop();
                    String pop8 = strings.pop();
                    strings.push(String.valueOf(Integer.parseInt(pop7) - Integer.parseInt(pop8)));
                    break;
                default:
                    strings.push(token);
            }
        }
        return Integer.parseInt(strings.get(0));
    }
}
